package net.zjitc.servlet;

import net.zjitc.domain.User;
import net.zjitc.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/27.
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("user");
        String password = request.getParameter("pass");
        UserService userService = new UserService();
        User user = null;
        try {
            user = userService.findByUsernameAndPassword(username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user != null) {
            response.sendRedirect("/login/success.jsp");
        } else {
            response.sendRedirect("/login/apply.jsp");

        }
    }
}
